import com.sun.javaws.IconUtil;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86156
 * Date: 2022-03-13
 * Time: 8:49
 */
class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}

public class LinkedList {
    public ListNode head;

    //Print every value in LinkedList
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //Verify that the linked list contains nodes with the given value
    public boolean containsVal(int val) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //Push a given node at the first position in LinkedList
    public void push_font(int val) {
        ListNode node = new ListNode(val);
        node.next = this.head;
        this.head = node;
    }

    //Push a given node at the last position in LinkedList
    public void push_back(int val) {
        ListNode node = new ListNode(val);
        ListNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //Delete the first node at the LinkedList
    public void pop_font() {
        if (this.head == null) {
            System.out.println("链表为空!无法删除");
        } else {
            this.head = this.head.next;
        }
    }

    //Delete the last node at the LinkedList
    public void pop_back() {
        if (this.head == null) {
            System.out.println("链表为空!无法删除!");
        } else {
            ListNode pre = this.findPreByIndex(this.size() - 1);
            pre.next = null;
        }
    }

    //Return the length of the LinkedList
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //Return the previous node at the given position
    public ListNode findPreByIndex(int index) {
        if (index < 0 || index >= this.size()) {
            System.out.print("位置不合法!");
            return null;
        }
        ListNode pre = this.head;
        while (index -1 != 0) {
            pre = pre.next;
            index--;
        }
        return pre;
    }

    //Return the preceding node of the node with specified value
    public ListNode findPreByVal(int val) {
        if (this.head == null) {
            System.out.println("链表为空!");
            return null;
        }
        ListNode pre = this.head;
        while (pre.next != null) {
            if (pre.next.val == val) {
                return pre;
            }
            pre = pre.next;
        }
        System.out.print("不存在含有值为 " + val + " 的节点!");
        return null;
    }

    //Insert a node at the given position
    public void insert(int index, int val) {
        if (index == 0) {
            this.push_font(val);
        } else if (index == this.size() -1) {
            this.push_back(val);
        } else {
            ListNode node = new ListNode(val);
            ListNode pre = findPreByIndex(index);
            if (pre == null) {
                return;
            }
            node.next = pre.next;
            pre.next = node;
        }
    }

    //Delete the first node with the given value
    public void removeByVal(int val) {
        ListNode pre = findPreByVal(val);
        if (pre == null) {
            System.out.println("无法删除!");;
        } else if (this.head.val == val) {
            this.head = this.head.next;
        } else {
            ListNode del = pre.next;
            pre.next = del.next;
        }
    }

    //Delete all nodes with the given value
    public void removeALLByVal(int val) {
        ListNode cur = this.head;
        ListNode curNext = cur.next;
        while (curNext != null) {
            if (curNext.val == val) {
                cur = curNext.next;
                curNext = curNext.next;
            } else {
                cur = curNext;
                curNext = curNext.next;
            }
        }
        if (this.head.val == val) {
            this.head = this.head.next;
        }
    }

    //Delete a node at the given position
    public void removeByIndex(int index) {
        ListNode pre = findPreByIndex(index);
        if (pre == null) {
            System.out.println("无法删除!");
        } else {
            ListNode cur = pre.next;
            pre.next = cur.next;
        }
    }

    //Clear the LinkedList
    public void clear() {
        while (this.head != null) {
            ListNode curNext = this.head.next;
            this.head.next = null;
            this.head = curNext;
        }
    }
}
